package com.yicong.ants.ui.coin;

import android.view.View;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.BR;
import com.yicong.ants.R;
import com.yicong.ants.bean.coin.YcRecord;
import com.yicong.ants.bean.global.ClientConfig;
import com.yicong.ants.databinding.Recycler2Binding;
import com.yicong.ants.manager.AdapterHelper;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.config.ZzConfigs;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * yc账户明细
 *
 * @author cch
 * @version 2020/7/15
 */
public class YcRecordActivity extends BaseTitleBarActivity<Recycler2Binding> implements View.OnClickListener {
    StatefulBindQuickAdapter<YcRecord> mAdapter;
    String mType = "";
    private int empty = 0;

    @Override
    protected int getLayout() {
        return R.layout.recycler_2;
    }

    @Override
    protected void initEventAndData() {
        setTitleBarVisible(false);
        mDataBind.setClick(this);
        AppHelper.transStatus(getWindow());
        BarUtils.setStatusBarLightMode(this, false);

        String total = getIntent().getStringExtra("total");
        mDataBind.title.setText("yc明细");
        mDataBind.des.setText("yc余额");
        mDataBind.total.setText(total);

        // 筛选
        mDataBind.rightText.setText("明细筛选");
        mDataBind.rightImage.setImageResource(R.mipmap.icon_me_screen);
        List<ClientConfig.Type_info> type_info = ZzConfigs.getClientConf().yc_type;
        int size = type_info.size();
        String[] title = new String[size + 1];
        String[] type = new String[size + 1];
        title[0] = "全部";
        type[0] = "";
        for (int i = 0; i < size; i++) {
            title[i + 1] = type_info.get(i).getTitle();
            type[i + 1] = type_info.get(i).getType();
        }
        mDataBind.right.setOnClickListener(view -> {
            String[] menuText = title;
            String[] ids = type;
            Dialogs.showBottomItemDialog(mContext, "筛选展示明细记录", menuText, (dialog, which) -> {
                mType = ids[which];
                onLoadData();
            });
        });
        initAdapter();
        onLoadData();
        initTitleChange();
    }

    private void initTitleChange() {
        mDataBind.mScrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
            float alp = scrollY * 0.02f;
            if (alp < 0) {
                alp = 0;
            } else if (alp > 1) {
                alp = 1;
            }
            mDataBind.titleBgImage.setAlpha(alp);
        });
    }

    @Override
    protected void onLoadData() {
        loadPage(1);
    }

    private void initAdapter() {
        mDataBind.recycler.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new StatefulBindQuickAdapter<YcRecord>(R.layout.yc_record_item, null) {
            @Override
            public void loadPageData(int page) {
                loadPage(page);
            }

            @Override
            protected void convert(DataBindViewHolder helper, YcRecord item) {
                helper.getBinding().setVariable(BR.bean, item);
                View view = helper.getBinding().getRoot();
                if (helper.getLayoutPosition() == 0){
                    view.setPadding(view.getPaddingLeft() , SizeUtils.dp2px(2), view.getPaddingRight(),view.getPaddingBottom());
                }else{
                    view.setPadding(view.getPaddingLeft() , SizeUtils.dp2px(12), view.getPaddingRight(),view.getPaddingBottom());
                }
            }
        };
        mAdapter.bindToRecyclerView(mDataBind.recycler);
    }

    void loadPage(final int page) {
        final Map<String, String> params = new HashMap<>();
        params.put("page", String.valueOf(page));
        params.put("type", String.valueOf(mType));

        if (page == 1) {
            mAdapter.setViewState(MultiStateView.VIEW_STATE_LOADING);
        }
        addSubscribe(RetrofitHelper.getApis().ycRecord(params)
                .compose(RxHelper.toMain())
                .subscribe(respBean -> {
                    if (page == 1) {
                        mAdapter.getData().clear();
                        mAdapter.notifyDataSetChanged();
                    }
                    if (respBean.isCodeFail()) {
                        AdapterHelper.solveFailResp(mDataBind.recycler, page, respBean);
                        if (empty == 0) {
                            empty = 1;
                            mAdapter.setEmptyView(R.layout.view_data_empty, mDataBind.recycler);
                        }
                        return;
                    }
                    List<YcRecord> data = respBean.getList();
                    mAdapter.solvePageData(data, page, respBean.getPageInfo().getCountPage());
                    if (empty == 0) {
                        empty = 1;
                        mAdapter.setEmptyView(R.layout.view_data_empty, mDataBind.recycler);
                    }
                }, RxHelper.getSwitchableErrorConsumer(mAdapter)));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        }
    }
}
